//
//  WZHPopView.swift
//  ShiGuangJiaSwift
//
//  Created by 汪志浩 on 2022/7/23.
//

import Foundation
import UIKit

public class WZHSheetView: UIView {
    weak var pop : OverlayController?
    
    ///总的容器
    @IBOutlet var content: UIStackView!
    ///顶部容器,包含 action 和 标题
    @IBOutlet weak var ctn_top: UIStackView!
    
    /**titile**/
    @IBOutlet weak var ctn_title: UIView!
    @IBOutlet weak var titlelab: UILabel!
    ///action 容器
    @IBOutlet weak var ctn_action: UIStackView!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    public convenience init(title : String? = nil ,_ actions :[WZHAction]){
        self.init(frame: CGRect(x: 0, y: 0, width:UIScreen.main.bounds.size.width - 30 , height: 100))
        
        
        if let title = title{
            self.titlelab.text = title
        }else{
            ctn_title.isHidden = true
        }
        
        
        actions.forEach{

            $0.select = {
                self.disMiss()
            }
            ctn_action.addArrangedSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.heightAnchor.constraint(equalToConstant: 50).isActive = true
            
        }
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    public  override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    public  func configUI() {
        
        let view = Bundle.main.loadNibNamed("WZHSheetView", owner: self)?.first as! UIView
        self.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        view.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        view.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
//        view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        
        ctn_top.layer.cornerRadius = 25
        ctn_top.layer.masksToBounds = true
        cancelBtn.layer.cornerRadius = 25
        cancelBtn.layer.masksToBounds = true
    }
    
    
    /**配置尺寸,,子类可重写**/
    public func config_size() -> CGSize{
        self.layoutIfNeeded()
        print(content.frame)
        return CGSize(width: content.frame.size.width, height:  content.frame.size.height + k_safeAreaInsets_Bottom)
    }
    /**配置pop,子类可重写**/
    public func config_pop(pop:OverlayController){
        pop.presentationStyle = .fromToBottom
        pop.layoutPosition =  .bottom
    }
    
    /**弹出**/
    public func show() {
        
        let pop = OverlayController.init(view: self, size: config_size())
        config_pop(pop:pop)
        self.pop = pop
        k_currentWindow!.present(overlay: self.pop!)
    }
    /**弹出在对应的View上**/
    public func showin(view:UIView){
        view.layoutIfNeeded()
        let pop = OverlayController.init(view: self, size: config_size())
        config_pop(pop:pop)
        self.pop = pop
        view.present(overlay: self.pop!)
    }
    
    /**消失**/
    public func disMiss(complete:(()->())? = nil) {
        self.pop?._dismiss(duration: 0.25, delay: 0, options:  [.curveEaseOut] ,completion: complete)
    }
    
    
    @IBAction func onCencel(_ sender: Any) {
        self.disMiss()
    }
}
